<?php
/**
 * LICENSE
 * 
 * [license information]
 * 
 * @category   Vi
 * @copyright  Copyright (c) 2009 visualidea.org
 * @license    http://license.visualidea.org
 * @version    v 1.0 2009-04-15
 */
require_once 'Vi/Model.php';
class Models_Portal extends Vi_Model
{ 
    /**
     * The primary key column or columns.
     * A compound key should be declared as an array.
     * You may declare a single-column primary key
     * as a string.
     *
     * @var mixed
     */
    protected $_primary = 'portal_id';
    
    /**
     * Constructor.
     *
     * Supported params for $config are:
     * - db              = user-supplied instance of database connector,
     *                     or key name of registry instance.
     * - name            = table name.
     * - primary         = string or array of primary key(s).
     * - rowClass        = row class name.
     * - rowsetClass     = rowset class name.
     * - referenceMap    = array structure to declare relationship
     *                     to parent tables.
     * - dependentTables = array of child tables.
     * - metadataCache   = cache for information from adapter describeTable().
     *
     * @param  mixed $config Array of user-specified config options, or just the Db Adapter.
     * @return void
     */
    public function __construct($config = array())
    {
        $this->_name = $this->_prefix . 'portal';
        return parent::__construct($config); 
    }
    /**
     * Get all portal's data
     *
     * Honors the Zend_Db_Adapter fetch mode.
     *
     * @param string|array                      $order    OPTIONAL An SQL ORDER clause.
     * @param int                               $count    OPTIONAL An SQL LIMIT count.
     * @param int                               $offset   OPTIONAL An SQL LIMIT offset.
     * @return array The row results per the Zend_Db_Adapter fetch mode.
     * 
     * @example $this->getAllPortal('portal_id ASC', 10, 0);
     */
    
    
    /**
     * Get all portals (admin)
     */
    public function getAllPortals($condition = array(), $order = null, $count = null, $offset = null)
    {
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('r' => $this->_name))
                ->joinLeft(array('u' => $this->_prefix . 'user'), 'r.user_id = u.user_id', array('full_name', 'email'))
                ->order($order)
                ->limit($count, $offset);
        /**
         * Conditions
         */
        if (null != @$condition['name']) {
            $select->where($this->getAdapter()->quoteInto('r.name LIKE ?', "%{$condition['name']}%"));
        }
        
        return $this->fetchAll($select)->toArray();
    }
    public function getPortalbyId($id)
    {
    	$select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('r' => $this->_name))
                ->WHERE('portal_id=?', $id);
        return $this->fetchAll($select)->toArray();   
    }
	public function getPortalbyAlias($alias)
    {
    	$select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('r' => $this->_name))
                ->WHERE('portal_alias=?', $alias);
        return $this->fetchAll($select)->toArray();     
    }
    //20110705
	public function getPortalbyUserName($userName)
    {
    	$select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('u' => $this->_prefix . 'user'))
                ->WHERE('username=?', $userName)
                ->join(array('r' => $this->_name), 'r.user_id = u.user_id');
                
        return $this->fetchAll($select)->toArray();     
    }
	public function getPortalbyUserId($userId)
    {
    	$select = $this->select()
                ->setIntegrityCheck(false)
                ->from(array('r' => $this->_name))
                ->WHERE('user_id=?', $userId);
                
        return $this->fetchAll($select)->toArray();     
    }
    //20110705
    
    //20110708
    public function getRecentPortal()
    {
    	$select = $this->select()
    	        ->setIntegrityCheck(false)
    	        ->from(array('r' => $this->_name))
                ->WHERE('generated=?', 1)
                ->order('created_date')
                ->limit(1);
                
        return $this->fetchAll($select)->toArray(); 
    }
    //20110708
    
    //20110718
    public function getPortalbyVacancyUpdatedDate()
    {
    	$select = $this->select()
    	        ->setIntegrityCheck(false)
    	        ->from(array('r' => $this->_name))
                ->WHERE('generated=?', 1)
                ->order('vacancy_updated_date')
                ->limit(1);
                
        return $this->fetchAll($select)->toArray(); 
    }
    //20110718
    
}